// models/userModel.js
const db = require('../db'); // 引入数据库连接

// 检查用户名是否存在
exports.checkUsernameExists = (username, callback) => {
  const query = 'SELECT COUNT(*) AS count FROM users WHERE username = ?';
  db.query(query, [username], (err, results) => {
    if (err) return callback(err);
    const exists = results[0].count > 0;
    callback(null, exists);
  });
};

// 创建新用户
exports.createUser = (username, hashedPassword, callback) => {
  const query = 'INSERT INTO users (username, password) VALUES (?, ?)';
  db.query(query, [username, hashedPassword], (err, results) => {
    if (err) return callback(err);
    callback(null, results);
  });
};
// 根据用户名获取用户信息
exports.getUserByUsername = (username, callback) => {
  const query = 'SELECT * FROM users WHERE username = ?';
  db.query(query, [username], (err, results) => {
    if (err) return callback(err);
    // 如果用户存在，返回用户信息；否则返回 null
    callback(null, results.length > 0 ? results[0] : null);
  });
};